import { createCipher, createDecipher } from 'crypto';

const key_crypto = 'D\xc7\xc3\xbf\x14`\xdb\xc1GJ\xce\xac\x10Q\x7f\xc4\xa7\xe7Sh\xfc\x9f;\xed\xea\x9f\xb9\xea\xb4\xc6\xb44\x95\x9d\xba\r\xcdA|\x01\xfd=\xc2\x13\x17\xba&\xb8\xc4\xc86y\x1dk\x94\xa6/8\x84\xe4d\xdc*\xff';

/**
 * 加密
 * @param {String} data
 */
export const encrypt = (data) => {
    const cipher = createCipher('aes-128-ecb', key_crypto);
    return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
};

/**
 * 解密
 * @param {String} data
 */
export const decrypt = (data) => {
    try {
        const decipher = createDecipher('aes-128-ecb', key_crypto);
        return decipher.update(data, 'hex', 'utf8') + decipher.final('utf8');
    } catch (e) {
        console.warn(`Decrypt error for ${data}: ${e}`);
    }
};
